package com.pengxun.mall.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.pengxun.mall.dao.GoodsDao;
import com.pengxun.mall.dao.GoodsGroupPriceDao;
import com.pengxun.mall.dto.GoodsDTO;
import com.pengxun.mall.dto.GoodsSearchDTO;
import com.pengxun.mall.model.Goods;
import com.pengxun.mall.model.GoodsGroupPrice;
import com.pengxun.mall.service.GoodsService;
import com.pengxun.mall.vo.GoodsGroupPriceVO;
import com.pengxun.mall.vo.GoodsVO;
import com.szeastroc.common.jdbc.crud.SqlBuilder;
import com.szeastroc.common.jdbc.page.Page;
import com.szeastroc.common.utils.CopyUtils;
import com.szeastroc.common.utils.ValidationUtils;

@Service
public class GoodsServiceImpl implements GoodsService {

	@Resource
	private GoodsDao goodsDao;

	@Resource
	private GoodsGroupPriceDao goodsGroupPriceDao;

	@Override
	public int insert(GoodsDTO goodsDTO) {
		ValidationUtils.validate(goodsDTO);
		Goods goods = new Goods();
		BeanUtils.copyProperties(goodsDTO, goods);
		goodsDao.insert(goods);
		if (!CollectionUtils.isEmpty(goodsDTO.getGoodsGroupPrices())) {
			goodsGroupPriceDao.batchInsert(goodsDTO.getGoodsGroupPrices().stream().map(p -> {
				GoodsGroupPrice goodsGroupPrice = new GoodsGroupPrice();
				BeanUtils.copyProperties(p, goodsGroupPrice);
				goodsGroupPrice.setGoodsId(goods.getId());
				return goodsGroupPrice;
			}).collect(Collectors.toList()));
		}

		return 0;
	}

	@Override
	public int batchInsert(List<GoodsDTO> goodsDTOs) {
		ValidationUtils.validate(goodsDTOs);
		if (CollectionUtils.isEmpty(goodsDTOs)) {
			return 0;
		}
		List<Goods> goodss = new ArrayList<Goods>();
		for (GoodsDTO goodsDTO : goodsDTOs) {
			Goods goods = new Goods();
			BeanUtils.copyProperties(goodsDTO, goods);
			goodss.add(goods);
		}
		return goodsDao.batchInsert(goodss);
	}

	@Override
	public int update(GoodsDTO goodsDTO) {
		ValidationUtils.validate(goodsDTO);
		Goods goods = new Goods();
		BeanUtils.copyProperties(goodsDTO, goods);
		goodsGroupPriceDao.deleteBy(new SqlBuilder(GoodsGroupPrice.class).eq("goodsId", goodsDTO.getId()).build());
		if (!CollectionUtils.isEmpty(goodsDTO.getGoodsGroupPrices())) {
			goodsGroupPriceDao.batchInsert(goodsDTO.getGoodsGroupPrices().stream().map(p -> {
				GoodsGroupPrice goodsGroupPrice = new GoodsGroupPrice();
				BeanUtils.copyProperties(p, goodsGroupPrice);
				goodsGroupPrice.setGoodsId(goods.getId());
				return goodsGroupPrice;
			}).collect(Collectors.toList()));
		}
		return goodsDao.updateBy(goods, "id");
	}

	@Override
	public int delete(Integer id) {
		if (id == null) {
			return 0;
		}
		Goods goods = new Goods();
		goods.setId(id);
		goodsGroupPriceDao.deleteBy(new SqlBuilder(GoodsGroupPrice.class).eq("goodsId", id).build());
		return goodsDao.delete(goods);
	}

	@Override
	public List<GoodsVO> findAll() {
		List<Goods> goodss = goodsDao.findAll(new SqlBuilder(Goods.class).build());
		List<GoodsVO> goodsVOs = new ArrayList<GoodsVO>();
		for (Goods goods : goodss) {
			GoodsVO goodsVO = new GoodsVO();
			BeanUtils.copyProperties(goods, goodsVO);
			List<GoodsGroupPrice> goodsGroupPrices = goodsGroupPriceDao
					.findAll(new SqlBuilder(GoodsGroupPrice.class).eq("goodsId", goods.getId()).build());
			if (!CollectionUtils.isEmpty(goodsGroupPrices)) {
				goodsVO.setGoodsGroupPrices(CopyUtils.copyList(goodsGroupPrices, GoodsGroupPriceVO.class));
			}
			goodsVOs.add(goodsVO);
		}
		return goodsVOs;
	}

	public Page findByPage(GoodsSearchDTO goodsSearchDTO) {
		List<Goods> goodss = goodsDao.find(goodsSearchDTO);
		List<GoodsVO> goodsVOs = new ArrayList<GoodsVO>();
		for (Goods goods : goodss) {
			GoodsVO goodsVO = new GoodsVO();
			BeanUtils.copyProperties(goods, goodsVO);
			goodsVOs.add(goodsVO);

			List<GoodsGroupPrice> goodsGroupPrices = goodsGroupPriceDao
					.findAll(new SqlBuilder(GoodsGroupPrice.class).eq("goodsId", goods.getId()).build());
			if (!CollectionUtils.isEmpty(goodsGroupPrices)) {
				goodsVO.setGoodsGroupPrices(CopyUtils.copyList(goodsGroupPrices, GoodsGroupPriceVO.class));
			}
		}

		Page page = new Page();
		BeanUtils.copyProperties(goodsSearchDTO, page);
		page.setData(goodsVOs);
		return page;
	};

	public GoodsVO findById(Integer id) {
		Goods goods = goodsDao.findFirst(new SqlBuilder(Goods.class).eq("id", id).build());
		GoodsVO goodsVO = new GoodsVO();
		List<GoodsGroupPrice> goodsGroupPrices = goodsGroupPriceDao
				.findAll(new SqlBuilder(GoodsGroupPrice.class).eq("goodsId", goods.getId()).build());
		if (!CollectionUtils.isEmpty(goodsGroupPrices)) {
			goodsVO.setGoodsGroupPrices(CopyUtils.copyList(goodsGroupPrices, GoodsGroupPriceVO.class));
		}
		BeanUtils.copyProperties(goods, goodsVO);

		return goodsVO;
	};

}
